/*  1:   */ package nl.siegmann.epublib.util;
/*  2:   */ 
/*  3:   */ import java.io.IOException;
/*  4:   */ import java.io.Reader;
/*  5:   */ import java.util.Scanner;
/*  6:   */ import java.util.regex.Matcher;
/*  7:   */ import java.util.regex.Pattern;
/*  8:   */ import nl.siegmann.epublib.domain.Resource;
/*  9:   */ import nl.siegmann.epublib.service.MediatypeService;
/* 10:   */ import org.apache.commons.lang.StringEscapeUtils;
/* 11:   */ import org.slf4j.Logger;
/* 12:   */ import org.slf4j.LoggerFactory;
/* 13:   */ 
/* 14:   */ public class ToolsResourceUtil
/* 15:   */ {
/* 16:38 */   private static Logger log = LoggerFactory.getLogger(ToolsResourceUtil.class);
/* 17:   */   
/* 18:   */   public static String getTitle(Resource resource)
/* 19:   */   {
/* 20:42 */     if (resource == null) {
/* 21:43 */       return "";
/* 22:   */     }
/* 23:45 */     if (resource.getMediaType() != MediatypeService.XHTML) {
/* 24:46 */       return resource.getHref();
/* 25:   */     }
/* 26:48 */     String title = findTitleFromXhtml(resource);
/* 27:49 */     if (title == null) {
/* 28:50 */       title = "";
/* 29:   */     }
/* 30:52 */     return title;
/* 31:   */   }
/* 32:   */   
/* 33:   */   public static String findTitleFromXhtml(Resource resource)
/* 34:   */   {
/* 35:66 */     if (resource == null) {
/* 36:67 */       return "";
/* 37:   */     }
/* 38:69 */     if (resource.getTitle() != null) {
/* 39:70 */       return resource.getTitle();
/* 40:   */     }
/* 41:72 */     Pattern h_tag = Pattern.compile("^h\\d\\s*", 2);
/* 42:73 */     String title = null;
/* 43:   */     try
/* 44:   */     {
/* 45:75 */       Reader content = resource.getReader();
/* 46:76 */       Scanner scanner = new Scanner(content);
/* 47:77 */       scanner.useDelimiter("<");
/* 48:78 */       while (scanner.hasNext())
/* 49:   */       {
/* 50:79 */         String text = scanner.next();
/* 51:80 */         int closePos = text.indexOf('>');
/* 52:81 */         String tag = text.substring(0, closePos);
/* 53:82 */         if ((tag.equalsIgnoreCase("title")) || (h_tag.matcher(tag).find()))
/* 54:   */         {
/* 55:85 */           title = text.substring(closePos + 1).trim();
/* 56:86 */           title = StringEscapeUtils.unescapeHtml(title);
/* 57:87 */           break;
/* 58:   */         }
/* 59:   */       }
/* 60:   */     }
/* 61:   */     catch (IOException e)
/* 62:   */     {
/* 63:91 */       log.error(e.getMessage());
/* 64:   */     }
/* 65:93 */     resource.setTitle(title);
/* 66:94 */     return title;
/* 67:   */   }
/* 68:   */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.util.ToolsResourceUtil
 * JD-Core Version:    0.7.0.1
 */